const {
  GiftShopModel
} = require('../../../../utils/giftshop-model.js');
var giftShopModel = new GiftShopModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAddressShow: true, //是否显示地址列表
    addressData: [], //地址列表
    type: null, //从订单过来
    memberAddressId:null,//选择的地址
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      memberAddressId:options.memberAddressId || null,
      type: options.type || null
    })
  },

  /**
   * 获取地址列表
   */
  getAddressList() {
    giftShopModel.addressList({
      merchantId: wx.getStorageSync('merchantId'),
    }).then(data => {
      var that = this;
      if (data && data.length > 0) {
        that.setData({
          addressData: data || [],
          isAddressShow: true, //是否显示地址列表
        })
      } else {
        that.setData({
          addressData: [],
          isAddressShow: false, //是否显示地址列表
        })
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 跳转到新建收货地址
   */
  goAddAddress() {
    wx.reportAnalytics('gift_address_add');
    wx.navigateTo({
      url: '/pages/giftShop/address/createAddress/index'
    })
  },

  /**
   * 跳转到修改收货地址
   */
  goEditAddress(e) {
    wx.reportAnalytics('gift_address_edit');
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/giftShop/address/createAddress/index?id=' + id
    })
  },

  //删除地址
  setAddressDelete(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '是否删除该地址？',
      cancelColor: '#333333', //取消文字的颜色
      confirmColor: '#333333', //确定文字的颜色
      confirmText: '确认删除',
      success(res) {
        if (res.confirm) {
          giftShopModel.addressDelete({
            merchantId: wx.getStorageSync('merchantId'),
            memberAddressId: id
          }).then(data => {
            wx.showToast({
              title: `删除成功！`,
              icon: 'none',
              duration: 2000
            })
            that.getAddressList()
          }).catch(err => {
            wx.showToast({
              title: `${err.error}`,
              icon: 'none',
              duration: 2000
            })
          })
        }
      }
    })
  },

  /**
   * 订单支付页面
   */
  goOrderPay(e) {
    if (this.data.type == 1) {
      let addressInfo = e.currentTarget.dataset.info //当前索引
      const pages = getCurrentPages()
      const prevPage = pages[pages.length - 2]
      prevPage.setData({
        memberAddress: addressInfo,
        isAddressShow:true,
      }, () => {
        prevPage.setBalanceInfo()
      })
      wx.navigateBack({
        delta: 1
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddressList()
  },
  /**
   * 手指触摸动作开始 记录起点X坐标
   */
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    this.data.addressData.forEach(function (v, i) {
      if (v.isTouchMove) { //只操作为true的
        v.isTouchMove = false;
      }
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      addressData: this.data.addressData
    })
  },

  /**
   * 滑动事件处理,一次只能滑出一个删除按钮
   */
  touchmove: function (e) {
    var that = this,
      addressId = e.currentTarget.dataset.id, //当前索引
      startX = that.data.startX, //开始X坐标
      startY = that.data.startY, //开始Y坐标
      touchMoveX = e.changedTouches[0].clientX, //滑动变化X坐标
      touchMoveY = e.changedTouches[0].clientY, //滑动变化Y坐标
      //获取滑动角度
      angle = that.angle({
        X: startX,
        Y: startY
      }, {
        X: touchMoveX,
        Y: touchMoveY
      });
    that.data.addressData.forEach(function (v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) {
        return;
      }
      if (v.memberAddressId == addressId) {
        if (touchMoveX > startX) { //右滑
          v.isTouchMove = false
        } else { //左滑
          v.isTouchMove = true
        }
      }
    })
    that.setData({
      addressData: that.data.addressData
    })
  },

  /**
   * 计算滑动角度
   * start 起点坐标
   * end 终点坐标　　* Math.PI 表示一个圆的周长与直径的比例，约为 3.14159;PI就是圆周率π，PI是弧度制的π,也就是180°   */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    if (this.data.type == 1 && this.data.addressData.length==0) {
      const pages = getCurrentPages()
      const prevPage = pages[pages.length - 2]
      prevPage.setData({
        memberAddress: {
          memberAddressId:''
        },
        isAddressShow:false,
      }, () => {
        prevPage.setBalanceInfo()
      })
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.data.type == 1 && this.data.addressData.length==0) {
      const pages = getCurrentPages()
      const prevPage = pages[pages.length - 2]
      prevPage.setData({
        memberAddress: {
          memberAddressId:''
        },
        isAddressShow:false,
      }, () => {
        prevPage.setBalanceInfo()
      })
    }
  },
})